#include<iostream>
#include<string>
#include<queue>
using namespace std;
typedef long long LL;
int n;
priority_queue<LL>q;
int main()
{
	cin >> n;
	int x;
	for (int i = 0; i < n; i++) {
		cin >> x;
		q.push(x);
	}
	LL ans = 0;
	while (q.size() != 1) {
		int x1 = q.top();
		q.pop();
		int x2 = q.top();
		q.pop();
		q.push(x1 + x2);
		ans += (LL)x1 * x2;
	}
	cout << ans << endl;
	return 0;
}